package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.controller.other.BaseController;
import com.example.domain.Dish;
import com.example.domain.Setmeal;
import com.example.domain.other.DishFlavor;
import com.example.domain.other.SetmealDish;
import com.example.dto.DishDto;
import com.example.dto.SetmealDishDto;
import com.example.result.JsonResult;
import com.example.service.SetmealDishService;
import com.example.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 *   套餐
 */
@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController extends BaseController {  //套餐
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;

    //新增套餐
    @PostMapping("/save")
    public JsonResult save(@RequestBody SetmealDishDto setmealDishDto){
        System.out.println(setmealDishDto.toString());

        LambdaQueryWrapper<Setmeal> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getName,setmealDishDto.getName());
        Setmeal one = setmealService.getOne(wrapper);
        if(one!=null){
            return new JsonResult(0,"此套餐已存在!");
        }

        Setmeal setmeal=setmealDishDto.gainSetmeal();
        boolean flag1 = setmealService.save(setmeal);
        if(!flag1){
            return new JsonResult(0,"套餐新增失败");
        }

        SetmealDish result=null;
        for(SetmealDish setmealDish: setmealDishDto.getSetMealDishList()){
            result=new SetmealDish();
            setmealDish.setSetmealId(setmeal.getId());
            setmealDish.setDishId(setmealDish.getDishId());
            setmealDish.setName(setmealDish.getName());
            setmealDish.setPrice(setmealDish.getPrice());
            boolean flag2 = setmealDishService.save(setmealDish);
            if(!flag2){
                return new JsonResult(0,"关联菜品新增失败");
            }
        }

        return new JsonResult(1,"新增成功");
    }
    //套餐分页查询
    @GetMapping("/page")
    public JsonResult page(Integer currentPage,Integer pageSize,String name){
        if(currentPage==null||pageSize==null){
            return new JsonResult(0,"当前页或者当前最大页条目数错误");
        }

        name="%"+name+"%";
        IPage<SetmealDishDto> setmealDtoIPage=new Page<>(currentPage,pageSize);
        setmealService.getSetmealDtoPage(setmealDtoIPage,name);
        return new JsonResult(1,"",setmealDtoIPage);
    }
    //启售菜品或停售菜品
    @PostMapping("/setStatus")
    public JsonResult setStatus(@RequestBody SetmealDishDto setmealDishDto){
        System.out.println(setmealDishDto.toString());
        Setmeal setmeal=new Setmeal();
        setmeal.setId(setmealDishDto.getSid());
        setmeal.setStatus(setmealDishDto.getStatus());
        if(setmeal.getStatus()==1){
            setmeal.setStatus(0);
        }else{
            setmeal.setStatus(1);
        }
        boolean flag = setmealService.updateById(setmeal);
        if(!flag){
            return new JsonResult(0,"修改失败,请重试!");
        }
        return new JsonResult(1,"操作成功");
    }
    //批量启售菜品或停售菜品
    @PostMapping("/statusList/{status}")
    public JsonResult statusList(@RequestBody String[] idsList,@PathVariable Integer status){
        for(String sid:idsList){
            Setmeal setmeal=new Setmeal();
            setmeal.setId(Long.valueOf(sid));setmeal.setStatus(status);
            boolean flag = setmealService.updateById(setmeal);
            if(!flag){
                return new JsonResult(0,"修改失败,请重试!");
            }
        }
        return new JsonResult(1,"操作成功");
    }
    //删除套餐
    @DeleteMapping("/deleteById/{id}")
    public JsonResult delete(@PathVariable Long id){
        if(id==null){
            return new JsonResult(0,"删除失败,数据异常");
        }
        boolean flag1 = setmealDishService.removeById(id);
        boolean flag2 = setmealService.removeById(id);
        if(!flag1||!flag2){
            return new JsonResult(0,"删除失败");
        }
        return new JsonResult(1,"删除成功");
    }
    //批量删除套餐
    @PostMapping("/deleteByIds")
    public JsonResult deleteByIds(@RequestBody List<String> idsList){
        List<Long> list=new ArrayList<>();
        for(String sid:idsList){
            list.add(Long.valueOf(sid));
        }
        boolean flag1 = setmealDishService.removeByDishId(list);
        if(!flag1){
            return new JsonResult(0,"删除关联口味失败,请重试!");
        }
        boolean flag2 = setmealService.removeByIds(list);
        if(!flag2){
            return new JsonResult(0,"删除菜品失败,请重试!");
        }
        return new JsonResult(1,"删除成功");
    }
    //根据id查询菜品信息和对应的口味信息
    @GetMapping("/gainByID/{id}")
    public JsonResult get(@PathVariable Long id){
        Setmeal setmeal = setmealService.getById(id);

        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        //合并
        SetmealDishDto setmealDishDto=new SetmealDishDto();
        setmealDishDto.gainSetmealDishDto(setmeal,list);

        return new JsonResult(1,"",setmealDishDto);
    }
    //修改菜品
    @PostMapping("/update")
    public JsonResult update(@RequestBody SetmealDishDto setmealDishDto){
        System.out.println(setmealDishDto.toString());
        Setmeal setmeal = setmealDishDto.gainSetmeal();
        setmeal.setId(setmealDishDto.getSid());      //方法中没有加入ID属性
        boolean flag2 = setmealService.updateById(setmeal);
        if(!flag2){
            return new JsonResult(0,"修改失败,请重试!");
        }
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        boolean flag1 = setmealDishService.remove(queryWrapper);
        if(!flag1){
            log.info("删除套餐关联数据失败!");
        }

        List<SetmealDish> list=setmealDishDto.getSetMealDishList();
        for(SetmealDish setmealDish:list){
            setmealDish.setSetmealId(setmealDishDto.getSid());
        }
        boolean flag3 = setmealDishService.saveBatch(setmealDishDto.getSetMealDishList());
        if(!flag3){
            return new JsonResult(0,"新增关联数据,请重试!");
        }

        return new JsonResult(1,"修改成功");
    }
    //根据条件查询套餐数据
    @GetMapping("/list")
    public JsonResult list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);

        return new JsonResult(1,"",list);
    }
}

